//
//  LookOverInfoCtry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2018/1/12.
//  Copyright © 2018年 LYH. All rights reserved.
//查看修改学员信息

#import "LookOverInfoCtry.h"
#import "JWLookSecondCry.h"//每一组显示VC
#import "JWUserManager.h"
#import "SDUserModel.h"
#import "JWStudentSearCtry.h"

#import "JWLookSDCtry.h"
@interface LookOverInfoCtry ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) SDUserModel *model;
@property (nonatomic, strong) NSMutableArray *SarryList;//总数据
@property (nonatomic, strong) NSMutableArray *GroupArry;//分好的组数据
@property (nonatomic, strong) UITableView *JStudentTab;//学员列表

@end

@implementation LookOverInfoCtry

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setdata];
   
}
//UItableView初始化
- (void)setupTable
{
    UITableView *tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //隐藏多余分割线
    tableView.tableFooterView=[[UIView alloc]init];
    tableView.backgroundColor=[UIColor whiteColor];
    tableView.showsHorizontalScrollIndicator=NO;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.dataSource=self;
    tableView.delegate=self;
tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:tableView];
    self.JStudentTab=tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏搜索
    UIImage *image=[UIImage imageNamed:@"searchbar_textfield_search_icon"];
    //取消渲染
    image=[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(didAction)];
   self.model=[JWUserManager SDme];
    self.navigationItem.title=@"查看/修改学员信息";
  
    
    [self setupTable];
    // Do any additional setup after loading the view.
}

-(void)didAction{
    JWStudentSearCtry *vc=[[JWStudentSearCtry alloc]init];
    //热搜
    vc.tagsArray=[[NSMutableArray alloc ]init];
    if(self.SarryList.count<7){
       vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];
        
    }else{
        for (int i=0; i<7; i++) {
            
            self.model=self.SarryList[i];
            [vc.tagsArray addObject:self.model.name];
        }
        
    }
    
    
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma -mark -----------------------------tableview datasource and delegate---------------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return  self.GroupArry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.创建cell
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
    NSMutableArray *arry=self.GroupArry[indexPath.section];
        self.model=arry[0];
    // 2.设置cell的数据
    cell.textLabel.text = [NSString stringWithFormat:@"第%@届学生",self.model.jieshu];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.JStudentTab deselectRowAtIndexPath:indexPath animated:YES];

    JWLookSecondCry *vc=[[JWLookSecondCry alloc]init];
    vc.SeDarry=self.GroupArry[indexPath.section];
    vc.title=@"学生列表";
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return  1;
}

-(void)setdata{
     [EasyShowLodingView showLodingText:@""];
    NSDictionary *dict=@{
                         @"jwid" : JWid,
                         @"jwpassword" : JWpassword,
                         };
//    参数  分页=>page 教务id=〉jwid ，秘码=>jwpassword
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_Studentlist parame:dict successData:^(id json) {
        [self.SarryList removeAllObjects];
     //   NSLog(@"--学生json--%@", json);
        NSArray * arr = json[@"data"];
        __weak typeof(self) weakslf = self;
        for(int i=0;i<arr.count;i++){
            self.model = [SDUserModel yy_modelWithJSON:arr[i]];
            [JWUserManager saveSD:self.model];
            [weakslf.SarryList addObject: weakslf.model ];
        }
#pragma -mark ————————————————————按届数分组————————————————————
        //届数数组
        NSMutableArray *jsArry=[NSMutableArray array];
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *currenDit=obj;
            NSString *jies=[currenDit objectForKey:@"jieshu"];
//            self.model=[SDUserModel yy_modelWithJSON:currenDit];
//            jies=self.model.jieshu;
            
            [jsArry addObject:jies];
        }];
        //去重届数
        NSSet *set=[NSSet setWithArray:jsArry];
        NSArray *userArry=[set allObjects];
        //yes升序排列，no,降序排列
        NSSortDescriptor *sd1=[NSSortDescriptor sortDescriptorWithKey:Nil ascending:YES];
        //得到升序排列的数组
        NSArray *myary=[userArry sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sd1, nil]];
        
        self.GroupArry=[NSMutableArray array];
        //遍历myary把_titleArray按照myary里的时间分成几个组每个组都是空的数组
        [myary enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSMutableArray *arr=[NSMutableArray array];
            [self.GroupArry addObject:arr];
        }];
        //遍历_SarryList取其中每个数据的届数看看与myary里的那个届数匹配就把这个数据装到_GroupArry 对应的组中
        [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *currentDict=obj;
            NSString *jies2=[currentDict objectForKey:@"jieshu"];
            for(NSString *str in myary)
            {
                if([str isEqualToString:jies2]){
                    NSMutableArray *arr=[self.GroupArry objectAtIndex:[myary indexOfObject:str]];
                    self.model=[SDUserModel yy_modelWithJSON:currentDict];
                    
                    [arr addObject:self.model];
                }
                
            }
            
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if(arr.count>0){
                
            }else{
                [self.JStudentTab removeFromSuperview];
                KongDataView *Kview=[KongDataView NilDataview];
                Kview.frame=self.view.frame;
                [self.view addSubview:Kview];
            }
            
            [EasyShowLodingView hidenLoding];
            [weakslf.JStudentTab reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
    }];
}

-(NSMutableArray *)GroupArry{
    if(_GroupArry==nil){
        _GroupArry=[NSMutableArray array];
    }
    return _GroupArry;
}
-(NSMutableArray *)SarryList{
    if(_SarryList==nil){
        _SarryList=[[NSMutableArray alloc]init];
    }
    return _SarryList;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
